package com.jiazhong.boot.redis.shuxin.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootTest
@Slf4j
public class HashApp {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        HashOperations<String, Object, Object> forHash = stringRedisTemplate.opsForHash();
        forHash.put("A", "a", "aa");
        forHash.put("A", "b", "21");
        forHash.put("A", "c", "kkl");
        Map<String, String> map = new HashMap<>();
        map.put("d", "dd");
        map.put("e", "ee");
        forHash.putAll("A", map);
        forHash.putIfAbsent("A", "a", "aa2");
        forHash.putIfAbsent("A", "k", "kk");

    }

    @Test
    public void b() {
        HashOperations<String, String, String> forHash = stringRedisTemplate.opsForHash();
        String a = forHash.get("A", "a");
        String a1 = forHash.randomKey("A");
        List<String> a2 = forHash.randomKeys("A", 3);
        Map.Entry<String, String> a3 = forHash.randomEntry("A");
        Map<String, String> a4 = forHash.randomEntries("A", 3);
        log.info("a:{}", a);
        log.info("a1:{}", a1);
        log.info("a2:{}", a2);
        log.info("a3:{}", a3);
        log.info("a4:{}", a4);
        Map<String, String> all = forHash.entries("A");
        log.info("all:{}", all);
        Set<String> keys = forHash.keys("A");
        log.info("keys:{}", keys);
        List<String> values = forHash.values("A");
        log.info("values:{}", values);
    }

    @Test
    public void c() {
        HashOperations<String, String, String> forHash = stringRedisTemplate.opsForHash();
        Long delete1 = forHash.delete("A", "k");
        Long delete2 = forHash.delete("A", "a");
        log.info("delete1:{}", delete1);
        log.info("delete2:{}", delete2);
        Boolean b1 = forHash.hasKey("A", "a");
        Boolean b2 = forHash.hasKey("A", "i");
        log.info("b1:{}", b1);
        log.info("b2:{}", b2);
        Long size = forHash.size("A");
        log.info("size:{}", size);
        forHash.increment("A", "b", 3);
        forHash.increment("A", "b", 4.5);
    }
}
